#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>

#define BUFF 1024

char * keys,*str;

void poly(char * str,char *key,int ch);

int main()
{
	str=(char*)malloc(BUFF);
	keys=(char*)malloc(26);
	int ch=0;

	printf("\nEnter the key:");
	scanf("%s",keys);
	while(1)
	{
		printf("\n--------Program menu--------");
		printf("\n1.Encrypt the data");
		printf("\n2.Decrypt the data");
		printf("\nEnter 0 to exit the program");
		printf("\nEnter yout choice:");
		scanf("%d",&ch);
		
		if(!ch) break;	
		printf("\nEnter the text:");
		getchar();
		scanf("%[^\n]",str);
		printf("\nU entered %s\n",str);
		poly(str,keys,ch);	
		printf("\nResult is :%s\n",str);
	}
}

void poly(char * str,char *key,int ch)
{
	int j=1;
	if(ch==2) j=-1;
	int len1=strlen(str);
	int len2=strlen(keys);
	int i=0;
	for(i=0;i<len1;i++)
	{
		int cur=str[i];
		cur=cur+(j*(key[i%len2]-97));
		if(cur>122) cur=96+(cur%122);
		if(cur<97) cur=123-(97-cur);
		str[i]=(char)cur;
		
	}
}
